package com.nosnow.ucenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.nosnow.common.utils.JwtUtils;
import com.nosnow.common.utils.MD5;
import com.nosnow.handler.SnowflakeException;
import com.nosnow.ucenter.entity.UcenterMember;
import com.nosnow.ucenter.entity.vo.LoginVo;
import com.nosnow.ucenter.entity.vo.RegisterVo;
import com.nosnow.ucenter.mapper.UcenterMemberMapper;
import com.nosnow.ucenter.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author bingheng
 * @since 2020-04-26
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public String login(LoginVo loginVo) {
        // 1. 校验手机号和密码非空
        if(StringUtils.isEmpty(loginVo.getMobile()) || StringUtils.isEmpty(loginVo.getPassword())) {
            throw new SnowflakeException(20001, "手机号或密码为空");
        }

        // 2. 校验手机号是否正确
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", loginVo.getMobile());
        UcenterMember ucenterMember = baseMapper.selectOne(wrapper);
        if(ucenterMember == null) {
            throw new SnowflakeException(20001, "手机号码不存在");
        }

        // 3. 判断密码是否正确(密文对比)
        if(!MD5.encrypt(loginVo.getPassword()).equals(ucenterMember.getPassword())) {
            throw new SnowflakeException(20001, "密码错误");
        }

        // 4. 判断用户是否被禁用
        if(ucenterMember.getIsDisabled()) {
            throw new SnowflakeException(20001, "您违规操作次数过多，被系统禁用了");
        }

        // 5. 使用JWT工具类生成一个Token字符串
        String jwtToken = JwtUtils.getJwtToken(ucenterMember.getId(), ucenterMember.getNickname());
        return jwtToken;
    }

    @Override
    public void register(RegisterVo registerVo) {
        // 1. 校验注册信息是否为空
        if(registerVo == null || StringUtils.isEmpty(registerVo.getMobile()) || StringUtils.isEmpty(registerVo.getCode())
        || StringUtils.isEmpty(registerVo.getNickname()) || StringUtils.isEmpty(registerVo.getPassword())) {
            throw new SnowflakeException(20001, "昵称，手机号，验证码，密码有空值");
        }

        // 2. 判断验证码是否合法
        String redisCode = redisTemplate.opsForValue().get(registerVo.getMobile());
        if(StringUtils.isEmpty(redisCode)) {
            throw new SnowflakeException(20001, "验证码已失效");
        }
        if(!registerVo.getCode().equals(redisCode)) {
            throw new SnowflakeException(20001, "验证码不正确");
        }

        // 3. 判断收好是否合法
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", registerVo.getMobile());
        Integer count = baseMapper.selectCount(wrapper);
        if(count > 0) {
            throw new SnowflakeException(20001, "用户已存在");
        }

        // 4. 将数据添加到数据库中
        UcenterMember ucenterMember = new UcenterMember();
        ucenterMember.setMobile(registerVo.getMobile());
        ucenterMember.setNickname(registerVo.getNickname());
        ucenterMember.setPassword(MD5.encrypt(registerVo.getPassword()));
        ucenterMember.setIsDisabled(false);
        ucenterMember.setAvatar("http://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eoj0hHXhgJNOTSOFsS4uZs8x1ConecaVOB8eIl115xmJZcT4oCicvia7wMEufibKtTLqiaJeanU2Lpg3w/132");
        baseMapper.insert(ucenterMember);
    }

    @Override
    public UcenterMember getOpenIdMember(String openid) {
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("openid", openid);
        return baseMapper.selectOne(wrapper);
    }

    @Override
    public Integer countRegisterDay(String day) {
        return baseMapper.countRegisterDay(day);
    }
}
